package org.hcyspartnoc.luoxin.controller.system;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.hcyspartnoc.luoxin.common.core.AjaxResult;
import org.hcyspartnoc.luoxin.common.core.PageBean;
import org.hcyspartnoc.luoxin.common.core.annotation.Log;
import org.hcyspartnoc.luoxin.common.core.annotation.PreAuthorize;
import org.hcyspartnoc.luoxin.common.core.enums.BusinessType;
import org.hcyspartnoc.luoxin.common.exception.common.CommonUtilsException;
import org.hcyspartnoc.luoxin.common.exception.framework.AuthException;
import org.hcyspartnoc.luoxin.common.exception.system.SysRoleException;
import org.hcyspartnoc.luoxin.common.exception.system.SysUserException;
import org.hcyspartnoc.luoxin.pojo.dto.system.*;
import org.hcyspartnoc.luoxin.pojo.po.system.SysUserPo;
import org.hcyspartnoc.luoxin.pojo.vo.system.SysUserVo;
import org.hcyspartnoc.luoxin.service.system.impl.SysUserServiceImpl;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Resource
    private SysUserServiceImpl sysUserService;

    @Log(title = "系统·用户·注册·微信", businessType = BusinessType.INSERT, isSaveResponseData = false)
    @PreAuthorize(auth = "system:user:register:wx", descr = "通过微信注册用户")
    @PostMapping("/register/wx")
    public AjaxResult registerByWx(@Valid @RequestBody SysUserWxRegisterDto sysUserWxRegisterDto) throws SysUserException {
        String token = sysUserService.registerByWx(sysUserWxRegisterDto);
        HashMap<String, String> resultMap = new HashMap<>();
        resultMap.put("token", token);
        return AjaxResult.success(resultMap);
    }


//    @Log(title = "系统·用户·注册·手机号", businessType = BusinessType.INSERT, isSaveResponseData = false)
//    @PreAuthorize(auth = "system:user:register:phone", descr = "通过手机号注册用户")
//    @PostMapping("/register/phone")
//    public AjaxResult registerByPhone(@Valid @RequestBody SysUserPhoneRegisterDto  sysUserPhoneRegisterDto) throws SysUserException {
//        String token = sysUserService.registerByPhone(sysUserPhoneRegisterDto);
//        HashMap<String, String> resultMap = new HashMap<>();
//        resultMap.put("token", token);
//        return AjaxResult.success(resultMap);
//    }

    @Log(title = "系统·用户·登录·手机号", businessType = BusinessType.OTHER, isSaveResponseData = false)
    @PreAuthorize(auth = "system:user:login:phone", descr = "通过手机号登录")
    @PostMapping("/login/phone")
    public AjaxResult loginByPhone(@Valid @RequestBody SysUserLoginByPhoneDto sysUserLoginByPhoneDto) throws SysUserException, CommonUtilsException {
        String token = sysUserService.loginByPhone(sysUserLoginByPhoneDto);
        HashMap<String, String> resultMap = new HashMap<>();
        resultMap.put("token", token);
        return AjaxResult.success(resultMap);
    }

    @Log(title = "系统·用户·修改·头像", businessType = BusinessType.UPDATE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:user:modify:avatar", descr = "用户修改头像")
    @PutMapping("/modify/avatar")
    public AjaxResult modifyAvatar(@NotNull @RequestParam MultipartFile image) throws CommonUtilsException, SysUserException {
        String resultURL = sysUserService.modifyAvatar(image);
        HashMap<String, String> resultMap = new HashMap<>();
        resultMap.put("url", resultURL);
        return AjaxResult.success(resultMap);
    }

    @Log(title = "系统·用户·修改·密码", businessType = BusinessType.UPDATE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:user:modify:pwd", descr = "用户修改密码")
    @PutMapping("/modify/pwd")
    public AjaxResult modifyPassword(@Valid @RequestBody SysUserModifyPwdDto sysUserModifyPwdDto) throws SysUserException {
        sysUserService.modifyPassword(sysUserModifyPwdDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "系统·用户·修改·基础信息", businessType = BusinessType.UPDATE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:user:modify:baseInfo", descr = "用户修改基础信息")
    @PutMapping("/modify/baseInfo")
    public AjaxResult modifyBaseInfo(@Valid @RequestBody SysUserModifyBaseInfoDto sysUserModifyBaseInfoDto) {
        sysUserService.modifyBaseInfo(sysUserModifyBaseInfoDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "系统·用户·查询·通过token", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "system:user:query:token", descr = "通过token获取用户信息")
    @GetMapping("/query/token")
    public AjaxResult queryByToken() {
        SysUserVo sysUserVo = sysUserService.queryByToken();
        return AjaxResult.success(sysUserVo);
    }

    @Log(title = "系统·用户·换绑·角色", businessType = BusinessType.UPDATE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:user:rebinding:role", descr = "换绑用户角色")
    @PutMapping("/rebinding/role")
    public AjaxResult rebindingRole(@Valid @RequestBody SysUserRebindingRoleDto sysUserRebindingRoleDto) throws SysUserException, SysRoleException {
        sysUserService.rebindingRole(sysUserRebindingRoleDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "系统·用户·修改·状态", businessType = BusinessType.UPDATE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:user:modify:status", descr = "修改用户状态")
    @PutMapping("/modify/status")
    public AjaxResult modifyStatus(@Valid @RequestBody SysUserModifyStatusDto sysUserModifyStatusDto) throws SysUserException, AuthException, SysRoleException {
        sysUserService.modifyStatus(sysUserModifyStatusDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "系统·用户·查询·分页", businessType = BusinessType.OTHER, isSaveResponseData = false)
    @PreAuthorize(auth = "system:user:query:page", descr = "分页查询用户")
    @GetMapping("/query/page")
    public AjaxResult queryPage(@Min(value = 1, message = "当前页不能小于1")
                                @RequestParam(defaultValue = "1") int currentPage,
                                @Min(value = PageBean.MIN_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                @Max(value = PageBean.MAX_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                @RequestParam(defaultValue = "5") int pageSize,
                                @Length(max = 10, message = "名称长度0-10")
                                @RequestParam(defaultValue = "") String nickname,
                                @Length(max = 11, message = "手机号格式错误")
                                @RequestParam(defaultValue = "") String phone,
                                @Min(value = 0, message = "状态值0-2")
                                @Max(value = 2, message = "状态值0-2")
                                @RequestParam(defaultValue = "0") Integer status,
                                @RequestParam(defaultValue = "0") Integer roleId) {
        PageBean<SysUserPo, SysUserVo> pageBean = sysUserService.queryPage(currentPage, pageSize, nickname, phone, status, roleId);
        return AjaxResult.success(pageBean);
    }

    @Log(title = "系统·用户·注销", businessType = BusinessType.DELETE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:user:logout", descr = "注销用户")
    @DeleteMapping("/logout")
    public AjaxResult logout() throws SysUserException {
        sysUserService.logout();
        return AjaxResult.successMsg();
    }
}
